package com.ruoyi.ggsearch.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ggsearch.domain.GgDomain;
import com.ruoyi.ggsearch.service.IGgDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 域名信息Controller
 * 
 * @author yangjin
 * @date 2023-04-07
 */
@RestController
@RequestMapping("/domain")
public class GgDomainController extends BaseController
{
    @Autowired
    private IGgDomainService ggDomainService;

    /**
     * 查询域名信息列表
     */
    @RequiresPermissions("ggsearch:domain:list")
    @GetMapping("/list")
    public TableDataInfo list(GgDomain ggDomain)
    {
        startPage();
        List<GgDomain> list = ggDomainService.selectGgDomainList(ggDomain);
        return getDataTable(list);
    }

    /**
     * 导出域名信息列表
     */
    @RequiresPermissions("ggsearch:domain:export")
    @Log(title = "域名信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GgDomain ggDomain)
    {
        List<GgDomain> list = ggDomainService.selectGgDomainList(ggDomain);
        ExcelUtil<GgDomain> util = new ExcelUtil<GgDomain>(GgDomain.class);
        util.exportExcel(response, list, "域名信息数据");
    }

    /**
     * 获取域名信息详细信息
     */
    @RequiresPermissions("ggsearch:domain:query")
    @GetMapping(value = "/{keyId}")
    public AjaxResult getInfo(@PathVariable("keyId") Long keyId)
    {
        return success(ggDomainService.selectGgDomainByKeyId(keyId));
    }

    /**
     * 新增域名信息
     */
    @RequiresPermissions("ggsearch:domain:add")
    @Log(title = "域名信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GgDomain ggDomain)
    {
        return toAjax(ggDomainService.insertGgDomain(ggDomain));
    }

    /**
     * 修改域名信息
     */
    @RequiresPermissions("ggsearch:domain:edit")
    @Log(title = "域名信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GgDomain ggDomain)
    {
        return toAjax(ggDomainService.updateGgDomain(ggDomain));
    }

    /**
     * 删除域名信息
     */
    @RequiresPermissions("ggsearch:domain:remove")
    @Log(title = "域名信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{keyIds}")
    public AjaxResult remove(@PathVariable Long[] keyIds)
    {
        return toAjax(ggDomainService.deleteGgDomainByKeyIds(keyIds));
    }

    @Log(title = "域名信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("ggsearch:domain:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GgDomain> util = new ExcelUtil<GgDomain>(GgDomain.class);
        List<GgDomain> domainList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        ggDomainService.deleteGgDomainAll();
        String message = ggDomainService.importDomain(domainList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<GgDomain> util = new ExcelUtil<GgDomain>(GgDomain.class);
        util.importTemplateExcel(response, "域名数据");
    }
}
